package com.bdqn.t330.controller;

import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class LoginSpringSecurityController {

    @RequestMapping("/dologin")
    public String dologin(HttpSession session, Model model) {
        SecurityContext context = SecurityContextHolder.getContext();
        //context == context_session  true
        //User user = new User();
        //user.setUsrName(context.getAuthentication().getName());
        session.setAttribute("user", context.getAuthentication().getName());
        model.addAttribute("userModel", context.getAuthentication().getName());
        return "success";
    }


    /**
     * 自定义登录地址
     * @param session
     * @param model
     * @return
     */
    @GetMapping("/login")
    public String login(HttpSession session, Model model) {
        return "login";
    }

    @RequestMapping("/admin")
    public String admin() {
        return "admin";
    }

    @RequestMapping("/dept")
    public String dept() {
        return "dept";
    }

}
